package com.yyge.controller.admin.contamer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.contamer.req.ContamerReq;
import com.yyge.controller.admin.contamer.resp.ContamerResp;
import com.yyge.entity.Contamer;
import com.yyge.service.ContamerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/contamer")
public class ContamerController extends BaseController<Contamer,Object> {
    // 注入业务逻辑层
    private ContamerService service;

    @Autowired/*自动装配*/
    public void setService(ContamerService contamerService){
        this.service=contamerService;
    }

    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<ContamerResp>> get(ContamerReq req) throws Exception  {
        QueryWrapper<Contamer> wrapper=this.getWrapper(req);
        List<Contamer> list = service.list(wrapper);

        // 将对象进行转换过滤无效条件
        PageResp pageResp =this.page(req,pageInfo ->service.page(pageInfo,wrapper));
        return R.okHasData(pageResp);
    }

//    @GetMapping("account/exists")/* GET(SELECT): 判断当前用户是否存在 */
//    //http://localhost:8080/api/front/contamer/account/exists?loginid=13139751326
//    public R<PageResp<ContamerResp>> get(ContamerReq req) throws Exception  {
//        QueryWrapper<Contamer> wrapper=this.getWrapper(this.converter,req);
//        List<Contamer> list = service.list(wrapper);
//
//        // 将对象进行转换过滤无效条件
//        PageResp pageResp =this.page(req.getPage(),req.getSize(),pageInfo ->service.page(pageInfo,wrapper));
//        return R.okHasData(pageResp);
//    }

    @PostMapping/* POST(CREATE): 新增&登陆 */
    public R post(@RequestBody Contamer contamer){
        Boolean i=service.save(contamer);
        return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody Contamer contamer){
        Boolean i=service.updateById(contamer);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody Contamer contamer){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }
}

